#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QPixmap>
#include <QLabel>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnchon_clicked()
{
    QString fname = QFileDialog::getOpenFileName();
    if(fname.isEmpty())
        return;
    ui->txtchonanh->setText(fname);
   // QPixmap pixmap(fname);
    //QLabel * label = new QLabel();
    //label->setPixmap(pixmap);
    //label->show();
}

// ****Thai đổi độ sáng tối ảnh mức xam.
void MainWindow::on_bntstaxam_clicked()
{
    QImage image_in(ui->txtchonanh->text()); //doc anh tu file anh
    //Tao anh ket qua có kich thuoc bang anh dau vao
    QImage image_out(image_in.width(),image_in.height(),QImage::Format_RGB32);
    int c=ui->txtmucxam->text().toInt(); //bien doi chuoi thanh so
    for(int x=0;x<image_in.width();x++)
        for (int y=0;y<image_in.height();y++){
         QRgb color = image_in.pixel(x,y); //lay mau cua pixel x,y
         int gray = qGray(color); //doi anh ve muc xam
         int out = gray+c; //thay doi do sang toi
         if(out >255) out = 255; //kiem tra out
         else if(out<0) out = 0; // out nam trong 0...255
         //Tao mau tu muc xam
         QRgb color_out = qRgb(out,out,out); //3 thanh phan giong nhau se co mau xam
         //Gan mau cho pixel(x,y) tren anh ket qua
         image_out.setPixel(x,y,color_out);
        }
  //Hien thi anh goc
    QLabel * label_in = new QLabel();
    //Doi image->piamap va gan cho label
    label_in->setPixmap(QPixmap::fromImage(image_in));
    //label_out->invertpixels();
    label_in->show();

    //hien thi anh xu ly
    QLabel * label_out = new QLabel();
    //Doi image->piamap va gan cho label
    label_out->setPixmap(QPixmap::fromImage(image_out));
    label_out->show();
   // QMessageBox::information(this, "Thong Bao","Thai doi do sang toi thanh cong" );

}
//*****Thay đổi độ sáng tối ảnh màu.
void MainWindow::on_bntastmau_clicked()
{
    QImage image_in(ui->txtchonanh->text()); //doc anh tu file anh
    //Tao anh ket qua có kich thuoc bang anh dau vao
    QImage image_out(image_in.width(),image_in.height(),QImage::Format_RGB32);
    int c=ui->txtmucxam->text().toInt(); //bien doi chuoi thanh so
    for(int x=0;x<image_in.width();x++)
        for (int y=0;y<image_in.height();y++){
         QRgb rgb = image_in.pixel(x,y); //lay mau cua pixel x,y
         QColor color(rgb);
         int h,s,v;
         //Lay 3 thanh phan H,S,V  cua color
         color.getHsv(&h,&s,&v);

         int out = v+c; //thay doi do sang toi
         if(out >255) out = 255; //kiem tra out
         else if(out<0) out = 0; // out nam trong 0...255
         //Tao mau tu muc xam
         QColor color_out = QColor::fromHsv(h,s,out); //3 thanh phan mau se thay doi do sang v
         //Gan mau cho pixel(x,y) tren anh ket qua
         image_out.setPixel(x,y,color_out.rgb());
        }
  //Hien thi anh goc
    QLabel * label_in = new QLabel();
    //Doi image->piamap va gan cho label
    label_in->setPixmap(QPixmap::fromImage(image_in));
    label_in->show();

    //hien thi anh xu ly
    QLabel * label_out = new QLabel();
    //Doi image->piamap va gan cho label
    label_out->setPixmap(QPixmap::fromImage(image_out));
    label_out->show();
}

void MainWindow::on_bntanhxam_pt_clicked()
{
    QImage image_in(ui->txtchonanh->text()); //doc anh tu file anh
    //Tao anh ket qua có kich thuoc bang anh dau vao
    QImage image_out(image_in.width(),image_in.height(),QImage::Format_RGB32);
    float c=ui->txtdtphan->text().toFloat(); //bien doi chuoi thanh so
    for(int x=0;x<image_in.width();x++)
        for (int y=0;y<image_in.height();y++){
         QRgb color = image_in.pixel(x,y); //lay mau cua pixel x,y
         int gray = qGray(color); //doi anh ve muc xam
         float out = gray*c; //thay doi do sang toi
         if(out >255) out = 255; //kiem tra out
         else if(out<0) out = 0; // out nam trong 0...255
         //Tao mau tu muc xam
         QRgb color_out = qRgb(out,out,out); //3 thanh phan giong nhau se co mau xam
         //Gan mau cho pixel(x,y) tren anh ket qua
         image_out.setPixel(x,y,color_out);
        }
  //Hien thi anh goc
    QLabel * label_in = new QLabel();
    //Doi image->piamap va gan cho label
    label_in->setPixmap(QPixmap::fromImage(image_in));
    label_in->show();

    //hien thi anh xu ly
    QLabel * label_out = new QLabel();
    //Doi image->piamap va gan cho label
    label_out->setPixmap(QPixmap::fromImage(image_out));
    label_out->show();
   // QImage image(ui->image_out->text());
    tochucdo(image_out,127);
}

void MainWindow::on_bntanhmau_tp_clicked()
{
    QImage image_in(ui->txtchonanh->text()); //doc anh tu file anh
    //Tao anh ket qua có kich thuoc bang anh dau vao
    QImage image_out(image_in.width(),image_in.height(),QImage::Format_RGB32);
    float c=ui->txtdtphan->text().toFloat(); //bien doi chuoi thanh so
    for(int x=0;x<image_in.width();x++)
        for (int y=0;y<image_in.height();y++){
         QRgb rgb = image_in.pixel(x,y); //lay mau cua pixel x,y
         QColor color(rgb);
         int h,s,v;
         //Lay 3 thanh phan H,S,V  cua color
         color.getHsv(&h,&s,&v);

         int out = v*c; //thay doi do sang toi
         if(out >255) out = 255; //kiem tra out
         else if(out<0) out = 0; // out nam trong 0...255
         //Tao mau tu muc xam
         QColor color_out = QColor::fromHsv(h,s,out); //3 thanh phan mau se thay doi do sang v
         //Gan mau cho pixel(x,y) tren anh ket qua
         image_out.setPixel(x,y,color_out.rgb());
        }
  //Hien thi anh goc
    QLabel * label_in = new QLabel();
    //Doi image->piamap va gan cho label
    label_in->setPixmap(QPixmap::fromImage(image_in));
    label_in->show();

    //hien thi anh xu ly
    QLabel * label_out = new QLabel();
    //Doi image->piamap va gan cho label
    label_out->setPixmap(QPixmap::fromImage(image_out));
    label_out->show();
}

void MainWindow::on_bntanhxam_pn_clicked()
{
    QImage image_in(ui->txtchonanh->text()); //doc anh tu file anh
    //Tao anh ket qua có kich thuoc bang anh dau vao
    QImage image_out(image_in.width(),image_in.height(),QImage::Format_RGB32);
    int c=ui->txtphannguong->text().toInt(); //bien doi chuoi thanh so
    for(int x=0;x<image_in.width();x++)
        for (int y=0;y<image_in.height();y++){
         QRgb color = image_in.pixel(x,y); //lay mau cua pixel x,y
         int gray = qGray(color); //doi anh ve muc xam
         float out = gray; //thay doi do sang toi
         if(out>c) out = 255; //kiem tra out
         else if(out<c) out = 0; // out nam trong 0...255
         //Tao mau tu muc xam
         QRgb color_out = qRgb(out,out,out); //3 thanh phan giong nhau se co mau xam
         //Gan mau cho pixel(x,y) tren anh ket qua
         image_out.setPixel(x,y,color_out);
        }
  //Hien thi anh goc
    QLabel * label_in = new QLabel();
    //Doi image->piamap va gan cho label
    label_in->setPixmap(QPixmap::fromImage(image_in));
    label_in->show();

    //hien thi anh xu ly
    QLabel * label_out = new QLabel();
    //Doi image->piamap va gan cho label
    label_out->setPixmap(QPixmap::fromImage(image_out));
    label_out->show();
}

void MainWindow::on_bntphannguonganhmau_clicked()
{
    QImage image_in(ui->txtchonanh->text()); //doc anh tu file anh
    //Tao anh ket qua có kich thuoc bang anh dau vao
    QImage image_out(image_in.width(),image_in.height(),QImage::Format_RGB32);
    int c=ui->txtphannguong->text().toInt(); //bien doi chuoi thanh so
    for(int x=0;x<image_in.width();x++)
        for (int y=0;y<image_in.height();y++){
        QRgb color = image_in.pixel(x,y); //lay mau cua pixel x,y




         int gray = qGray(color); //doi anh ve muc xam
         float out = gray; //thay doi do sang toi
         if(out>c) out = 255; //kiem tra out
         else if(out<c) out = 0; // out nam trong 0...255
         //Tao mau tu muc xam
         QRgb color_out = qRgb(out,out,out); //3 thanh phan giong nhau se co mau xam
         //Gan mau cho pixel(x,y) tren anh ket qua
         image_out.setPixel(x,y,color_out);

        }
  //Hien thi anh goc
    QLabel * label_in = new QLabel();
    //Doi image->piamap va gan cho label
    label_in->setPixmap(QPixmap::fromImage(image_in));
    label_in->show();

    //hien thi anh xu ly
    QLabel * label_out = new QLabel();
    //Doi image->piamap va gan cho label
    label_out->setPixmap(QPixmap::fromImage(image_out));
    label_out->show();
}
void MainWindow::on_btnchonanh1_clicked()
{
    QString fname = QFileDialog::getOpenFileName();
    if(fname.isEmpty())
        return;
    ui->txtchonanh1->setText(fname);
}

void MainWindow::on_bntchonanh2_clicked()
{
    QString fname = QFileDialog::getOpenFileName();
    if(fname.isEmpty())
        return;
    ui->txtchonanh2->setText(fname);
}

void MainWindow::on_bntconganh_clicked()
{
    QImage anh1(ui->txtchonanh1->text());
    QImage anh2(ui->txtchonanh2->text());
     float tile = ui->txtopcciti->text().toFloat();
     QImage imagecong(anh1.width(),anh1.height(),QImage::Format_RGB32);
     for(int x=0; x < anh1.width();x++)
         for(int y=0; y < anh1.height();y++){
             QColor color1 = anh1.pixel(x,y);
             QColor color2 = anh2.pixel(x,y);
             int imgRed = color1.red()*tile + color2.red()*(1-tile);
             int imgGreen = color1.green()*tile + color2.green()*(1-tile);
             int imgblue = color1.blue()*tile + color2.blue()*(1-tile);
             imagecong.setPixel(x,y,qRgb(imgRed,imgGreen,imgblue));



         }

     //Hien thi anh goc
       QLabel * label_in1 = new QLabel();
       //Doi image->piamap va gan cho label
       label_in1->setPixmap(QPixmap::fromImage(anh1));
       label_in1->show();

       QLabel * label_in2 = new QLabel();
       //Doi image->piamap va gan cho label
       label_in2->setPixmap(QPixmap::fromImage(anh2));
       label_in2->show();

       //hien thi anh xu ly
       QLabel * label_out = new QLabel();
       //Doi image->piamap va gan cho label
       label_out->setPixmap(QPixmap::fromImage(imagecong));
       label_out->show();
}

void MainWindow::on_bnttruanh_clicked()
{
    QImage anh1(ui->txtchonanh1->text());
    QImage anh2(ui->txtchonanh2->text());
     float tile = ui->txtopcciti->text().toFloat();
     QImage imagetru(anh1.width(),anh1.height(),QImage::Format_RGB32);
     for(int x=0; x < anh1.width();x++)
         for(int y=0; y < anh1.height();y++){
             QColor color1 = anh1.pixel(x,y);
             QColor color2 = anh2.pixel(x,y);
             int imgRed = color1.red()*tile - color2.red()*(1-tile);
             int imgGreen = color1.green()*tile - color2.green()*(1-tile);
             int imgblue = color1.blue()*tile - color2.blue()*(1-tile);
             imagetru.setPixel(x,y,qRgb(imgRed,imgGreen,imgblue));



         }

     //Hien thi anh goc
       QLabel * label_in1 = new QLabel();
       //Doi image->piamap va gan cho label
       label_in1->setPixmap(QPixmap::fromImage(anh1));
       label_in1->show();

       QLabel * label_in2 = new QLabel();
       //Doi image->piamap va gan cho label
       label_in2->setPixmap(QPixmap::fromImage(anh2));
       label_in2->show();

       //hien thi anh xu ly
       QLabel * label_out = new QLabel();
       //Doi image->piamap va gan cho label
       label_out->setPixmap(QPixmap::fromImage(imagetru));
       label_out->show();
}

void MainWindow::tochucdo(const QImage &img, const int &his_height ){
    int h[256];
    for(int i=0; i<256;i++)
        h[i]=0;
    for(int x=0; x<img.width();x++)
        for(int y=0; y<img.height();y++){
            QRgb color = img.pixel(x,y);
            int gray = qGray(color);
            h[gray]++;
        }
            int max = 0;
            for(int i=0; i<256;i++)
                if(h[i]>max) max=h[i];
            QImage img_his = QImage(256,his_height,QImage::Format_RGB888);
            img_his.fill(Qt::black);
            int lineHeight;
            for(int i=0; i<256;i++){
                lineHeight = his_height*h[i]/max;
                for(int j=his_height-1;j>=his_height-1-lineHeight;j--)
                    img_his.setPixel(i,j,qRgb(0,0,255));
            }
            QImage image_out(img.width(),img.height(),QImage::Format_RGB888);
            QLabel* label_in = new QLabel();
            label_in->setPixmap(QPixmap::fromImage(img));
            label_in->show();
            QLabel* label_out = new QLabel();
            label_out->setPixmap(QPixmap::fromImage(img_his));
            label_out->show();

            }

void MainWindow::on_btnchon_2_clicked()
{
    QImage image(ui->txtchonanh_tcd->text());
    tochucdo(image,127);
}

void MainWindow::on_bntchon_tcd_clicked()
{
    QString fname = QFileDialog::getOpenFileName();
    if(fname.isEmpty())
        return;
    ui->txtchonanh_tcd->setText(fname);
}


void MainWindow::tochucdoanhmau(const QImage &image, const int &his_height ){

    QImage image_his1 =QImage(256,his_height,QImage::Format_RGB32);
    QImage image_his2 =QImage(256,his_height,QImage::Format_RGB32);
    QImage image_his3 =QImage(256,his_height,QImage::Format_RGB32);
    image_his1.fill(Qt::white);
    image_his2.fill(Qt::white);
    image_his3.fill(Qt::white);
    int hr[256],hg[256],hb[256];
    for(int i=0;i<256;i++)
        hr[i] = hg[i] = hb[i] = 0;
    QColor color;
    for(int x=0;x<image.width();x++)
        for(int y=0;y<image.height();y++){
            color = image.pixel(x,y);
            hr[color.red()]++;
            hg[color.green()]++;
            hb[color.blue()]++;
        }
    int max=0;
        for(int i=0;i<256;i++){
            if(hr[i]>max) max =hr[i];
            if(hg[i]>max) max =hg[i];
            if(hb[i]>max) max =hb[i];
        }

    int lineHeight;
    for(int i=0;i<256;i++){
        lineHeight = his_height *hr[i]/max;
        for(int j=his_height-1;j>his_height-1-lineHeight;j--)
            image_his1.setPixel(i,j,qRgb(255,0,0));

        lineHeight = his_height *hg[i]/max;
        for(int j=his_height-1;j>his_height-1-lineHeight;j--)
            image_his2.setPixel(i,j,qRgb(0,255,0));

        lineHeight = his_height *hb[i]/max;
        for(int j=his_height-1;j>his_height-1-lineHeight;j--)
        image_his3.setPixel(i,j,qRgb(0,0,255));
    }
    QLabel * label_in = new QLabel();
    label_in->setPixmap(QPixmap::fromImage(image));
    label_in->show();

    QLabel * label_out1 = new QLabel();
    label_out1->setPixmap(QPixmap::fromImage(image_his1));
   // label_out->setWindowTitle(QFileInfo(ui->lineEdit->text()).fileName());
    label_out1->show();
    QLabel * label_out2 = new QLabel();
    label_out2->setPixmap(QPixmap::fromImage(image_his2));
   // label_out->setWindowTitle(QFileInfo(ui->lineEdit->text()).fileName());
    label_out2->show();
    QLabel * label_out3 = new QLabel();
    label_out3->setPixmap(QPixmap::fromImage(image_his3));
   // label_out->setWindowTitle(QFileInfo(ui->lineEdit->text()).fileName());
    label_out3->show();
}
void MainWindow::on_bntanhmau_tcd_clicked()
{
    QImage image(ui->txtchonanh_tcd->text());
    tochucdoanhmau(image,128);
}



void MainWindow::on_bnt_anhamban_clicked()
{
    QImage image_in(ui->txtchonanh->text()); //doc anh tu file anh
    //Tao anh ket qua có kich thuoc bang anh dau vao
    QImage image_out(image_in.width(),image_in.height(),QImage::Format_RGB32);
    for(int x=0;x<image_in.width();x++)
        for (int y=0;y<image_in.height();y++){
         QRgb color = image_in.pixel(x,y); //lay mau cua pixel x,y
         int gray = qGray(color); //doi anh ve muc xam
         float out = 256-1-gray; //thay doi do sang toi
         if(out<0) out = 0; //kiem tra out

         //Tao mau tu muc xam
         QRgb color_out = qRgb(out,out,out); //3 thanh phan giong nhau se co mau xam
         //Gan mau cho pixel(x,y) tren anh ket qua
         image_out.setPixel(x,y,color_out);
        }
  //Hien thi anh goc
    QLabel * label_in = new QLabel();
    //Doi image->piamap va gan cho label
    label_in->setPixmap(QPixmap::fromImage(image_in));
    label_in->show();

    //hien thi anh xu ly
    QLabel * label_out = new QLabel();
    //Doi image->piamap va gan cho label
    label_out->setPixmap(QPixmap::fromImage(image_out));
    label_out->show();
}

void MainWindow::on_bnt_anhamban_anhmau_clicked()
{   QImage image_in(ui->txtchonanh->text()); //doc anh tu file anh
    QImage image_out(image_in.width(),image_in.height(),QImage::Format_RGB32);
    for(int x=0;x<image_in.width();x++)
        for (int y=0;y<image_in.height();y++){
             QColor color1 = image_in.pixel(x,y);

             int imgRed =255-color1.red();
             int imgGreen = 255-color1.green();
             int imgblue =255- color1.blue();
               if(imgRed<0) imgRed = 0; //kiem tra out
                 if(imgblue<0) imgblue = 0; //kiem tra out
                   if(imgGreen<0) imgGreen = 0; //kiem tra out
             image_out.setPixel(x,y,qRgb(imgRed,imgGreen,imgblue));

         }
    //Hien thi anh goc
      QLabel * label_in = new QLabel();
      //Doi image->piamap va gan cho label
      label_in->setPixmap(QPixmap::fromImage(image_in));
      label_in->show();

      //hien thi anh xu ly
      QLabel * label_out = new QLabel();
      //Doi image->piamap va gan cho label
      label_out->setPixmap(QPixmap::fromImage(image_out));
      label_out->show();


}
